package com.itheima.tlias84.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.tlias84.dto.SelectAllEmpDTO;
import com.itheima.tlias84.dto.SelectEmpDTO;
import com.itheima.tlias84.dto.SelectEmpDetailDTO;
import com.itheima.tlias84.dto.SelectExprDTO;
import com.itheima.tlias84.entity.Emp;
import com.itheima.tlias84.entity.Experience;
import com.itheima.tlias84.mapper.EmpMapper;
import com.itheima.tlias84.mapper.ExprMapper;
import com.itheima.tlias84.po.InsertEmpParam;
import com.itheima.tlias84.po.SelectEmpPageParam;
import com.itheima.tlias84.po.UpdateEmpParam;
import com.itheima.tlias84.service.EmpService;
import com.itheima.tlias84.utils.ResultUtil;
import com.itheima.tlias84.vo.PageVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class EmpServiceImpl implements EmpService {

    @Resource
    EmpMapper empMapper;
    @Resource
    ExprMapper exprMapper;

    @Override
    public ResultUtil selectEmpByPage(SelectEmpPageParam param) {

        //注意pageHelper会自动执行一个count sql  ，并且会自动拼接limit
        //因此必须下载自己sql之前
//        param.setPage((param.getPage()-1)*param.getPageSize());
        Page<Object> page = PageHelper.startPage(param.getPage(), param.getPageSize());

        List<SelectEmpDTO> selectEmpDTOS = empMapper.selectEmpByPage(param);
        PageVO<SelectEmpDTO> pageVO = new PageVO<>();
        pageVO.setTotal(page.getTotal());
        pageVO.setRows(selectEmpDTOS);
//        PageInfo pageInfo = new PageInfo();
//        pageInfo.setList(selectEmpDTOS);
//        pageInfo.setTotal(page.getTotal());
        return ResultUtil.success(pageVO);
    }

//    @Override
//    public int insertOne(Emp emp) {
//        return empMapper.insertOne(emp);
//
//    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int batchDelete(List<Integer> ids) {
        //删除履历
        int i = empMapper.batchDelete(ids);

        int i1 = exprMapper.batchDeleteEspr(ids);
        log.info("删除{}条履历", i1);
        return i;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertEmp(InsertEmpParam param) {
        param.setCreateTime(LocalDateTime.now());
        param.setUpdateTime(LocalDateTime.now());
        //插入工作履历'
        log.info("员工回显的主键{}", param.getId());
        int flag = empMapper.insertEmp(param);
        List<Experience> exprList = param.getExprList();

        if (CollectionUtils.isNotEmpty(exprList)) {
            exprList = exprList.stream().map(t -> {
                t.setEmpId(param.getId());
                t.setCreateTime(LocalDateTime.now());
                t.setUpdateTime(LocalDateTime.now());
                return t;
            }).toList();
            int i = exprMapper.batchInsertExpr(exprList);
            log.info("插入员工履历{}", i);
        }


        return flag;
    }

    @Override
    public SelectEmpDetailDTO selectEmpByid(Integer id) {
        List<Experience> experiences = exprMapper.selectExprByEmpId(id);
        SelectEmpDetailDTO selectEmpDetailDTO = empMapper.selectEmpById(id);
        selectEmpDetailDTO.setExprList(experiences);
        return selectEmpDetailDTO;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateEmp(UpdateEmpParam param) {

        param.setUpdateTime(LocalDateTime.now());
        //先更新员工表信息
        int i = empMapper.updateEmp(param);
        //再删除员工的履历
        int i1 = exprMapper.batchDeleteEspr(List.of(param.getId()));
        log.info("删除旧履历{}", i1);

        //新增所有履历
        List<Experience> exprList = param.getExprList();
        if (CollectionUtils.isNotEmpty(exprList)) {
            exprList = exprList.stream().map(t -> {
                t.setEmpId(param.getId());
                t.setCreateTime(LocalDateTime.now());
                t.setUpdateTime(LocalDateTime.now());
                return t;
            }).toList();
            int i2 = exprMapper.batchInsertExpr(exprList);
            log.info("插入员工履历{}", i2);
        }

        return i;
    }

    //查询所有emp作为list的接口
    @Override
    public List<SelectAllEmpDTO> selectAllEmp() {
        return empMapper.selectAllEmp();
    }
}
